package com.tengju.data.infrastructure.repository.clickhouse.ods;

import com.google.common.collect.Lists;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.clickhouse.OdsBigDataAchievementBonusDiDRepository;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.ods.OdsBigDataAchievementBonusDiDMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class OdsBigDataAchievementBonusDiDRepositoryImpl implements OdsBigDataAchievementBonusDiDRepository {

    @Resource
    private OdsBigDataAchievementBonusDiDMapper odsBigDataAchievementBonusDiDMapper;

    @Override
    public List<Row> getMonthMarketClassifyRatio(String startVisitDate,String endVisitDate, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return Lists.newArrayList();
        }
        List<Map<String,Object>> mapList =
                odsBigDataAchievementBonusDiDMapper.getMonthMarketClassifyRatio(startVisitDate,
                        endVisitDate,
                        userIdCode.getValue());
        if (CollectionUtils.isEmpty(mapList)) {
            return Lists.newArrayList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public Row getMonthAdvertiseIndex(String startVisitDate, String endVisitDate, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return new Row();
        }
        Map<String,Object> map =
                odsBigDataAchievementBonusDiDMapper.getMonthAdvertiseIndex(startVisitDate,
                        endVisitDate,
                        userIdCode.getValue());
        return new Row(map);
    }

    @Override
    public List<Row> getMonthDateMarketTendency(String startVisitDate,String endVisitDate, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return Lists.newArrayList();
        }
        List<Map<String,Object>> mapList =
                odsBigDataAchievementBonusDiDMapper.getMonthDateMarketTendency(startVisitDate,
                        endVisitDate,
                        userIdCode.getValue());
        if (CollectionUtils.isEmpty(mapList)) {
            return Lists.newArrayList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> getMonthDateMarketDetail(String startVisitDate,String endVisitDate, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return Lists.newArrayList();
        }
        List<Map<String,Object>> mapList =
                odsBigDataAchievementBonusDiDMapper.getMonthDateMarketDetail(startVisitDate,
                        endVisitDate,
                        userIdCode.getValue());
        if (CollectionUtils.isEmpty(mapList)) {
            return Lists.newArrayList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> getYearMonthMarketTendency(Integer yearStartMonth, Integer yearEndMonth, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return Lists.newArrayList();
        }
        List<Map<String,Object>> mapList =
                odsBigDataAchievementBonusDiDMapper.getYearMonthMarketTendency(yearStartMonth,
                        yearEndMonth,
                        userIdCode.getValue());
        if (CollectionUtils.isEmpty(mapList)) {
            return Lists.newArrayList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> getIndexByTeamRatioType(String startVisitDate,String endVisitDate, UserIdCode userIdCode) {
        if (userIdCode == null) {
            return Lists.newArrayList();
        }
        List<Map<String,Object>> mapList =
                odsBigDataAchievementBonusDiDMapper.getIndexByTeamRatioType(startVisitDate,
                        endVisitDate,
                        userIdCode.getValue());
        if (CollectionUtils.isEmpty(mapList)) {
            return Lists.newArrayList();
        }
        return mapList.stream()
                .filter(Objects::nonNull)
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public Row getUpdateTime(UserIdCode userIdCode) {
        if (userIdCode == null) {
            return new Row();
        }
        Map<String, Object> updateTime = odsBigDataAchievementBonusDiDMapper.getUpdateTime(userIdCode.getValue());
        return new Row(updateTime);
    }
}
